# -*- coding: utf-8 -*-
# windows 下如果出现编码问题，将 utf-8 改为 cp936
import os
def findfile(key, inputdir='.'):
    found_list = []
    # os.walk 获取指定目录下的所有深度的文件、子目录的列表
    for path, dirnames, filenames in os.walk(inputdir):
        print ('searching', path, '...')
        for name in filenames:
            full_name = path + '/' + name
            if key in name:  # 如果文件名中有关键字
                found_list.append(full_name)
                f=open(full_name,encoding ='UTF-8')
                errors='ignore'
                for l in f.readlines():
                    if key in l:  # 如果当前行中有关键字
                        found_list.append(full_name + ' : ' + l)

    return found_list

#/Users/yinhaodong/Desktop/cs
# 输入搜索关键字和路径
keyword = input('search:')
path = input('in:')
if not path.strip():
    path = '.'

result = findfile(keyword, path)

print ('\n========== result ===========\n\n')
for r in result:
    print (r)